﻿using AutomatedTestDataManagement.Models;
using System.Data;
using System.Linq;
using System.Web.Mvc;

namespace AutomatedTestDataManagement.Controllers
{
    public class TestController : Controller
    {
        private ApplicationTestDataEntities db = new ApplicationTestDataEntities();

        //
        // GET: /Test/

        public ActionResult Index()
        {
            return View(db.Tests.ToList());
        }

        //
        // GET: /Test/Details/5

        public ActionResult Details(int id = 0)
        {
            Test test = db.Tests.Find(id);
            if (test == null)
            {
                return HttpNotFound();
            }
            return View(test);
        }

        //
        // GET: /Test/Create

        public ActionResult Create(int id = 0, int suiteID = 0)
        {
            if (id != 0) { ViewBag.TestScenarioID = id; }
            if (suiteID != 0) { ViewBag.SuiteID = suiteID; }
            return View();
        }

        //
        // POST: /Test/Create

        [HttpPost]
        public ActionResult Create(Test test)
        {
            if (ModelState.IsValid)
            {
                db.Tests.Add(test);
                db.SaveChanges();

                if (Request.Form["SuiteID"] != null)
                {
                    ViewBag.SuiteID = Request.Form["SuiteID"];
                }

                if (Request.Form["ScenarioID"] != null)
                {
                    return RedirectToAction("Create", "ScenariosTest", new { id = Request.Form["ScenarioID"] });
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }

            return View(test);
        }

        //
        // GET: /Test/Edit/5

        public ActionResult Edit(int id = 0, int scenarioID = 0, int suiteID = 0)
        {
            Test test = db.Tests.Find(id);
            if (test == null)
            {
                return HttpNotFound();
            }

            if (scenarioID != 0)
            {
                ViewBag.TestScenarioID = scenarioID;
            }

            if (suiteID != 0)
            {
                ViewBag.SuiteID = suiteID;
            }

            test.TestTestSteps = test.TestTestSteps.OrderBy(t => t.StepNumber).ToList();
            return View(test);
        }

        //
        // POST: /Test/Edit/5

        [HttpPost]
        public ActionResult Edit(Test test)
        {
            if (Request.Form["SuiteID"] != null)
            {
                ViewBag.SuiteID = Request.Form["SuiteID"];
            }


            if (ModelState.IsValid)
            {
                db.Entry(test).State = EntityState.Modified;
                db.SaveChanges();

                foreach (TestTestStep testStep in test.TestTestSteps)
                {
                    db.Entry(testStep).State = EntityState.Modified;
                    db.SaveChanges();
                }

                if (Request.Form["ScenarioID"] != null)
                {
                    return RedirectToAction("Edit", "TestScenario", new { id = Request.Form["ScenarioID"] });
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }

            if (Request.Form["ScenarioID"] != null)
            {
                ViewBag.ScenarioID = Request.Form["ScenarioID"];
            }
            return View(test);
        }

        //
        // GET: /Test/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Test test = db.Tests.Find(id);
            if (test == null)
            {
                return HttpNotFound();
            }
            if (test.TestTestSteps.Count > 0)
            {
                ModelState.AddModelError("", "This test has associated test steps. You must remove all linked test steps before deleting");
            }
            if (test.TestScenarioTests.Count > 0)
            {
                ModelState.AddModelError("", "This test is used in at least one test scenario. You must remove it from all test scenarios before deleting");
            }
            return View(test);
        }

        //
        // POST: /Test/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Test test = db.Tests.Find(id);
            db.Tests.Remove(test);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}